Skip to content

Scope break values to loop bodies - #501

Merged
sinsoku merged 1 commit into
ruby:masterfrom
pvcresin:fix-loop-break-scope
Oct 31, 2026
Merged

sinsoku merged 1 commit into
ruby:masterfrom
pvcresin:fix-loop-break-scope

Conversation

@pvcresin

@pvcresin pvcresin commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A break inside a while nested in an Array#each block is currently connected to the block's break vertex. For foo(flag), TypeProf infers (:b | Array[Integer]), as if each could return the loop's break value.

This is not an infinite loop: when flag is truthy, the body breaks immediately; otherwise, the while exits without entering the body. Array#each should therefore retain its Array[Integer] return type.

Give each loop body its own break vertex and restore the enclosing vertex after analysis so a loop-local break cannot escape the surrounding block.

Test

Ran the focused scenario set: break_in_loop.rb, break.rb, break2.rb, and loop.rb (4 scenarios, 9 assertions).

@sinsoku sinsoku left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay. It looks good.

@sinsoku
sinsoku merged commit 3471536 into ruby:master Oct 31, 2026
6 checks passed
@pvcresin
pvcresin deleted the fix-loop-break-scope branch October 31, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants